<template>
  <ContentWrap>
    <div class="flex justify-end mb-4">
      <el-button type="primary" @click="handleAdd">新增套餐</el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column prop="name" label="套餐" align="center" />
      <el-table-column prop="commandsStr" label="指令" align="center" />
      <el-table-column prop="trialed" label="是否开启体验" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.trialed" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="subTitle" label="副标题" align="center" />
      <el-table-column prop="packageName" label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" size="small" link @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getPackageList"
    />
  </ContentWrap>
  <AddPackage
    ref="addPackageRef"
    v-if="isViewPackage"
    @close="isViewPackage = false"
    :editInfo="editInfo"
    @submit="handleSubmit"
  />
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import AddPackage from './addPackage.vue'
import { SofaBasicApi } from '@/api/application/sofa'
const tableData = ref([])
const loading = ref(false)
const isViewPackage = ref(false)

const queryParams = ref({
  pageNo: 1,
  pageSize: 10
})
const message = useMessage()
const total = ref(0)
const editInfo = ref<any>({})
const handleAdd = () => {
  editInfo.value = {}
  isViewPackage.value = true
}
const handleEdit = (row: any) => {
  editInfo.value = row
  isViewPackage.value = true
}
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定删除该套餐吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await SofaBasicApi.deletePackage({ id: row.id })
    message.success('删除成功')
    getPackageList()
  })
}
const handleSubmit = async (data: any) => {
  console.log(data)
  if (editInfo.value.id) {
    await SofaBasicApi.updatePackage(data)
    message.success('更新成功')
  } else {
    await SofaBasicApi.createPackage(data)
    message.success('新增成功')
  }
  getPackageList()
  isViewPackage.value = false
}
const getPackageList = async () => {
  loading.value = true
  try {
    const data = await SofaBasicApi.getPackagePage({
      ...queryParams.value
    })
    tableData.value = data.list
    total.value = data.total
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}
onMounted(() => {
  getPackageList()
})
</script>
<style lang="scss" scoped></style>
